#pragma once

#include <QString>
#include <QByteArray>
#include <QIcon>
#include <QUuid>
#include <QDateTime>
#include <QDebug>
#include <QFileInfo>
#include <QFile>


namespace model {

////////////////////////////////
/// 工具函数信息
////////////////////////////////
//日志宏
static inline QString getFileName(const QString& path)
{
    QFileInfo info(path);
    return info.fileName();
}

//把时间戳转化为 01-01 18:00:00 这样的格式
//在.h中定义的函数需要用 static or inline 修饰， 或者两个都用
static inline QString formatTime(qint64 timestamp)
{
    QDateTime dateTime = QDateTime::fromSecsSinceEpoch(static_cast<time_t>(timestamp));
    return dateTime.toString("MM-dd HH:mm:ss");
}

static inline qint64 getTime()
{
    return QDateTime::currentSecsSinceEpoch();
}

#define FILEINFO QString("[%1:%2]").arg(model::getFileName(__FILE__), QString::number(__LINE__))
#define TIMEINFO QString("[%1]").arg(model::formatTime(model::getTime()))
#define LOG() qDebug().noquote() << FILEINFO << TIMEINFO



//QByteArray转QIcon
static inline QIcon makeIcon(const QByteArray& byteArray)
{
    QPixmap map;
    map.loadFromData(byteArray);
    QIcon icon(map);
    return icon;
}

//获取文件的二进制内容
static inline QByteArray loadFileToQByteArray(const QString& fileName)
{
    QFile file(fileName);
    bool ok = file.open(QFile::ReadOnly);
    if(!ok)
    {
        LOG() << "文件打开失败";
        return QByteArray();
    }
    QByteArray byteArray = file.readAll();
    file.close();
    return byteArray;
}

//将二进制内容写入文件
static inline void writeByteArrayToFile(const QString& fileName, const QByteArray& byteArray)
{
    QFile file(fileName);
    file.open(QFile::WriteOnly);
    if(!file.isOpen())
    {
        LOG() << "文件打开失败";
        return;
    }
    file.write(byteArray);
    file.flush(); //保证内容真实写入到了文件中
    file.close();
}

////////////////////////////////
/// 用户信息
////////////////////////////////
class UserInfo
{
public:
    QString userId = "";            //用户编号
    QString nickName = "";          //用户姓名
    QString description = "";       //用户签名
    QString phone = "";             //电话
    QIcon icon;                     //用户头像
};

////////////////////////////////
/// 消息信息
////////////////////////////////
///消息类型
enum MessageType{
    TEXT_TYPE,        //文本消息
    FILE_TYPE,        //文件消息
    IMAGE_TYPE,       //图片消息
    SPEECH_TYPE       //语音消息
};

class Message
{
public:
    QString messageId = "";               //消息标号
    QString sessionId = "";                     //所属会话编号
    QString time = "";       //格式化的时间
    MessageType messageType = TEXT_TYPE;        //消息类型
    UserInfo sendr;                             //发送消息的用户信息
    QByteArray content;                         //消息的主要内容
    QString fileId = "";                        //用于保存文件的编号， 文本文件为"" , 其余文件设置对应的编号
    QString fileName = "";                      //其余文件为"", 文本文件设置为对应的文件名

private:
    static QString makeId()
    {
        return "M" + QUuid::createUuid().toString().sliced(25, 12); //取最后的12为作为标识
    }
public:
    static Message makeMessage(MessageType messageType, const QString& chatSessionId,
                    UserInfo sendr, const QByteArray& content, const QString& supplyInfo)
    {
        if(messageType == TEXT_TYPE)
        {
            return makeTextMessage(chatSessionId, sendr, content);
        }
        else if(messageType == FILE_TYPE)
        {
            return makeFileMessage(chatSessionId, sendr, content, supplyInfo);
        }
        else if(messageType == IMAGE_TYPE)
        {
            return makeImageMessage(chatSessionId, sendr, content);
        }
        else if(messageType == SPEECH_TYPE)
        {
            return makeSpeechMessage(chatSessionId, sendr, content);
        }
        else
        {
            return Message();
        }
    }
    static Message makeFileMessage(const QString& chatSessionId, UserInfo sendr, const QByteArray& content, const QString& fileName)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.messageType = FILE_TYPE;
        message.time = formatTime(getTime());
        message.sendr = sendr;
        message.content = content;
        message.fileId = "";
        message.fileName = fileName;
        return message;
    }
    static Message makeTextMessage(const QString& chatSessionId, UserInfo sendr, const QByteArray& content)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.messageType = TEXT_TYPE;
        message.time = formatTime(getTime());
        message.sendr = sendr;
        message.content = content;
        return message;
    }
    static Message makeImageMessage(const QString& chatSessionId, UserInfo sendr, const QByteArray& content)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.messageType = IMAGE_TYPE;
        message.time = formatTime(getTime());
        message.sendr = sendr;
        message.content = content;
        message.fileId = "";
        message.fileName = "";
        return message;
    }
    static Message makeSpeechMessage(const QString& chatSessionId, UserInfo sendr, const QByteArray& content)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.messageType = SPEECH_TYPE;
        message.time = formatTime(getTime());
        message.sendr = sendr;
        message.content = content;
        message.fileId = "";
        message.fileName = "";
        return message;
    }
};

////////////////////////////////
/// 会话信息
////////////////////////////////

class ChatSessionInfo
{
private:
    QString chatSessionId = "";           //会话编号
    QString chatSessionName = "";         //会话名称
    Message lastMessage;                  //最后一条消息，用于会话列表的显示
    QIcon icon;                           //会话图标
    QString userId = "";                  //如果是群聊userId为"" , 单聊的话为对方用户的id
};
} // end model
